set -x

PID_FILE=pid

function kill_running() {

  if [ ! -e "${PID_FILE}" ]
  then
    return
  fi

  # Pid file exists, but is the process running?
  PID=$(cat "${PID_FILE}")
  ps -p "${PID}" > /dev/null 2>&1
  IS_RUNNING=$?

  if [ "${IS_RUNNING}" -eq 0 ]
  then
    #PID=$(cat "${PID_FILE}")
    kill -9 "${PID}" >/dev/null 2>&1
    rm -f "${PID_FILE}"
  fi

  # The pid in the pidfile is not running. Remove the pidfile.
  rm -f "${PID_FILE}"
}
